// 引入 react 依赖
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Text, View, Image, TouchableOpacity, ImageBackground, ScrollView, InteractionManager } from 'react-native';

// 引入第三方依赖
import _ from 'lodash';
import pureRender from 'pure-render-decorator';
import { Actions } from 'react-native-router-flux';
import Icon from 'react-native-vector-icons/Ionicons';
import fs from 'react-native-fs';


// antd 按需加载
import { Tabs, Toast, Portal } from '@ant-design/react-native';

// 引入 处理数据 所需文件
import { billList, billDetail, touristBill } from '../../module/module.js'

// 引入样式
import Style from './style/style.js';

// 引入公用组件
import { Status, NavBar, CacheImage, Layout } from '../../components/index.js'

// 引入专用组件
import { } from './components'

// 引入工具包
import { } from '../../util/images.js';
import { press, size, ScreenWidth, WeChatShareToSession, pathHead, path, defaultDataList, onEndReached, onRefresh, concatDataList, defaultPage, ImageFileSave } from '../../util/utils.js'

class InviteFriends extends Component {

  constructor(props) {
    super(props)
    this.state = {
      bill: null
    }
  }


  componentDidMount() {
    InteractionManager.runAfterInteractions(() => {
      // ...耗时较长的同步的任务...
      this.loading = Toast.loading('加载中')
      billList().then(res => {
        this.setState({
          bill: res.data[0]
        })
        Portal.remove(this.loading)
      }).finally(() => {
        Portal.remove(this.loading)
      })
    });
  }

  async touristBill() {
    const { bill } = this.state
    const { loginInfo, user } = this.props

    WeChatShareToSession({
      type: 'imageUrl',
      title: 'image file download from network',
      description: 'share image file to time line',
      mediaTagName: 'email signature',
      messageAction: undefined,
      messageExt: undefined,
      imageUrl: `${path}/tourist/bill/${user.accountNo}/${bill.id}` // require the prefix on both iOS and Android platform
      // imageUrl: "file://" + res.path() // require the prefix on both iOS and Android platform
    }).then(shareRes => {
      console.log(shareRes)
    }).catch(error => {
      Toast.info(error)
      console.log(error)
    })

    console.log(`${path}/tourist/bill/${user.accountNo}/${bill.id}`)
    // ImageFileSave(`${path}/tourist/bill/${user.accountNo}/${bill.id}`).then(res => {
    //   console.log(res)
    //   WeChatShareToSession({
    //     type: 'imageFile',
    //     title: 'image file download from network',
    //     description: 'share image file to time line',
    //     mediaTagName: 'email signature',
    //     messageAction: undefined,
    //     messageExt: undefined,
    //     imageUrl: `${path}/tourist/bill/${user.accountNo}/${bill.id}` // require the prefix on both iOS and Android platform
    //     // imageUrl: "file://" + res.path() // require the prefix on both iOS and Android platform
    //   }).then(shareRes => {
    //     console.log(shareRes)
    //   }).catch(error => {
    //     console.log(error)
    //   })
    // }).catch(error => {
    //   console.log(error, '')
    // })
  }


  render() {
    const { bill } = this.state
    const { billList } = this.props
    return (
      <Layout
        style={{ backgroundColor: '#fff' }}
        header={<Status />}
        footer={
          <View style={{
            backgroundColor: '#fff'
          }}>
            <ScrollView
              horizontal={true}
              style={{
                flexDirection: 'row',
                paddingHorizontal: 10,
              }}>
              {billList.map((item, index) => {
                return (
                  <TouchableOpacity
                    key={index}
                    onPress={() => {
                      this.setState({
                        bill: item
                      })
                    }}
                  >
                    <View
                      key={index}
                      style={{
                        alignItems: 'center',
                        justifyContent: 'center',
                        padding: 10,
                        paddingBottom: 5,
                      }}
                    >
                      <CacheImage
                        resizeMod={"contain"}
                        style={[{
                          width: 60,
                          height: 60,
                          borderRadius: 30,
                          overflow: 'hidden'
                        }, bill && bill.id == item.id ? {
                          borderWidth: 1,
                          borderColor: '#000000',
                          width: 60,
                          height: 60,
                        } : {}]}
                        source={{ uri: `${pathHead}${item.img}` }}

                      />
                      <Text style={[{
                        fontSize: 14,
                        color: '#61676F',
                        lineHeight: 26,
                        marginTop: 2
                      }, bill && bill.id == item.id ? {
                        color: '#000000',
                      } : {}]}>{item.name}</Text>
                    </View>
                  </TouchableOpacity>
                )
              })}
            </ScrollView>
          </View>
        }
      >
        <NavBar
          title="邀请好友"
          back={true}
          extra={'分享'}
          extraOnPress={() => {
            this.touristBill()
          }}
        />
        <View style={{ flex: 1, overflow: 'hidden', alignItems: 'center', justifyContent: 'center', ...Style.content }}>
          {bill && (
            <CacheImage
              resizeMod={"contain"}
              style={{
                width: size(500),
                height: size(800)
              }}
              source={{ uri: `${pathHead}${bill.img}` }}
            />
          )}
        </View>
      </Layout >
    )
  }
}


export default connect(({ user, loginInfo, billList }) => ({
  user, loginInfo, billList
}))(pureRender(InviteFriends))
